package com.heer.gymnasium.activity;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/3/28.
 */

public class ResetPassWordActivity extends BaseActivity {
    @BindView(R.id.edit_password_old)
    EditText editPasswordOld;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.edit_password_confirm)
    EditText editPasswordConfirm;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_reset_password);
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @OnClick(R.id.btn_submit)
    public void onClick() {
        String mPswOld = editPasswordOld.getText().toString().trim();
        String mPassword = editPassword.getText().toString().trim();
        String mPswConfirm = editPasswordConfirm.getText().toString().trim();
        if ((mPassword.length() >= 6 && mPassword.length() <= 18)
                || (mPswOld.length() >= 6 && mPswOld.length() <= 18)
                || (mPswConfirm.length() >= 6 && mPswConfirm.length() <= 18)) {
            if (TextUtils.equals(mPassword, mPswConfirm)) {
                mAppHelper.apiService().updateUserPassword(mPswOld, mPassword, mPswConfirm).enqueue(new BaseCallback<ApiResponse>() {
                    @Override
                    public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                        super.onResponse(call, response);
                        if(response.isSuccessful()&&!response.body().isSccessful()){
                            new SVProgressHUD(mContext).showErrorWithStatus(response.body().getMsg());
                            return;
                        }
                        if (response.isSuccessful() && response.body().isSccessful()) {
                            new SVProgressHUD(mContext).showSuccessWithStatus("修改密码成功");
                        }
                        if(response.body().isTokenExpire()){
                            toLogin();
                        }
                    }

                    @Override
                    public void onFailure(Call<ApiResponse> call, Throwable t) {
                        super.onFailure(call, t);
                    }
                });
            } else {
                new SVProgressHUD(mContext).showErrorWithStatus(getString(R.string.password_error_consistent));
            }
        } else {
            new SVProgressHUD(mContext).showErrorWithStatus(getString(R.string.password_error));
        }
    }
}
